"""
初始化
"""
import json

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from conf import settings
from db.excel_handler import read
import logging.config
import jsmin
from threading import current_thread

logging.config.dictConfig(settings.LOGGING_DIC)  # 导入上面定义的logging配置
logger = logging.getLogger("初始化")  # 生成一个log实例

# JSON配置字典
config_dict = {}


def init_settings():
    """
    初始化配置文件
    """
    with open(settings.JSON_FILE, 'r', encoding='utf-8') as f:
        global config_dict
        config_dict.update(json.loads(jsmin.jsmin(f.read())))
    logger.info('settings.json文件初始化')
    # print(config_dict)


def host_init_interface():
    """
    初始化主机列表
    :return: 主机列表
    """
    host_list = read()
    if len(host_list) == 0:
        logger.error(f"主机列表为空，请重新填写{settings.WORKBOOK}")
        exit(1)
    logger.info(f"主机列表初始化成功，共有 {len(host_list)} 台主机等待操作")
    return host_list


def browser_init_interface():
    """
    初始化浏览器驱动
    :return: （浏览器对象，隐式等待对象）
    """
    options = webdriver.EdgeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-logging"])
    browser = webdriver.Edge(options=options, executable_path=settings.DRIVER_PATH)
    browser.maximize_window()
    # browser.minimize_window()

    OPEN_URL_STR = config_dict.get('WEB').get('OPEN_URL')
    OPEN_URL = config_dict.get('WEB').get('ALL_URL').get(OPEN_URL_STR)

    browser.get(OPEN_URL)
    # 显示等待
    wait = WebDriverWait(browser, 10, 0.5)
    logger.info(f"浏览器{current_thread().name}驱动初始化成功")
    return browser, wait


if __name__ == "__main__":
    pass
    # print(logger.name)
    init_settings()
    print(config_dict)
    # OPEN_URL_STR = config_dict.get('WEB').get('OPEN_URL')
    # print(OPEN_URL_STR)
    # print(config_dict.get('WEB').get(OPEN_URL_STR))

    USER = config_dict.get('WEB').get('USER')
    PASSWORD = config_dict.get('WEB').get('PASSWORD')
    print(USER)
    print(PASSWORD)
